home *** CD-ROM | disk | FTP | other *** search
/ 17 Bit Software 6: Level 6 / 17 Bit - Level 6 (1998)(Epic Marketing)[!].iso / quartz / q0709.dms / q0709.adf / Graphics / LowLevelGraphics / Example2.c < prev    next >
C/C++ Source or Header  |  1992-07-29  |  9KB  |  252 lines

  1. /***********************************************************/
  2. /*                                                         */
  3. /* Amiga C Encyclopedia (ACE) V3.0      Amiga C Club (ACC) */
  4. /* -------------------------------      ------------------ */
  5. /*                                                         */
  6. /* Book:    ACM Graphics                Amiga C Club       */
  7. /* Chapter: LowLevelGraphics            Tulevagen 22       */
  8. /* File:    Example2.c                  181 41  LIDINGO    */
  9. /* Author:  Anders Bjerin               SWEDEN             */
  10. /* Date:    92-04-28                                       */
  11. /* Version: 1.00                                           */
  12. /*                                                         */
  13. /*   Copyright 1992, Anders Bjerin - Amiga C Club (ACC)    */
  14. /*                                                         */
  15. /* Registered members may use this program freely in their */
  16. /*     own commercial/noncommercial programs/articles.     */
  17. /*                                                         */
  18. /***********************************************************/
  19.  
  20. /* This example shows how to create a large Raster and a smaller        */
  21. /* display. We fill the Raster with a lot of pixels in seven different  */
  22. /* colours and by altering the RxOffset and RyOffset values in the      */
  23. /* RasInfo structure, the Raster is scrolled in all directions. This    */
  24. /* method to scroll a large drawing in full speed is used in many games */
  25. /* and was even used in my own racing game "Car".                       */
  26.  
  27.  
  28. #include <intuition/intuition.h>
  29. #include <graphics/gfxbase.h>
  30.  
  31.  
  32. #define RWIDTH   450 /* Raster 450 pixels wide.  */
  33. #define RHEIGHT  250 /* Raster 250 lines high.   */ 
  34.  
  35. /* The ViewPort is quite small, and is placed in the middle of the View: */
  36. #define DWIDTH   200 /* Display 200 pixels wide. */ 
  37. #define DHEIGHT  100 /* Display 100 lines high.  */
  38. #define DXOFFSET  60 /* DxOffset 60 pixels.      */
  39. #define DYOFFSET  50 /* DyOffset 50 lines.       */
  40.  
  41. #define DEPTH      3 /* 3 BitPlanes should be used, gives eight colours. */
  42. #define COLOURS    8 /* 2^3 = 8                                          */
  43.  
  44. #define SPEED      1 /* How many pixels the Raster should be scrolled */
  45.                      /* every time.                                   */
  46.  
  47.  
  48. struct IntuitionBase *IntuitionBase;
  49. struct GfxBase *GfxBase;
  50.  
  51.  
  52. struct View my_view;
  53. struct View *my_old_view;
  54. struct ViewPort my_view_port;
  55. struct RasInfo my_ras_info;
  56. struct BitMap my_bit_map;
  57. struct RastPort my_rast_port;
  58.  
  59.  
  60. UWORD my_color_table[] =
  61. {
  62.   0x000, /* Colour 0, Black       */
  63.   0x800, /* Colour 1, Red         */
  64.   0xF00, /* Colour 2, Light red   */
  65.   0x080, /* Colour 3, Green       */
  66.   0x0F0, /* Colour 4, Light green */
  67.   0x008, /* Colour 5, Blue        */
  68.   0x00F, /* Colour 6, Light Blue  */
  69.   0xFFF, /* Colour 7, White       */
  70. };
  71.  
  72.  
  73. void clean_up();
  74. void main();
  75.  
  76.  
  77. void main()
  78. {
  79.   SHORT deltaX = SPEED;
  80.   SHORT deltaY = SPEED;
  81.   UWORD *pointer;
  82.   int loop;
  83.   
  84.   
  85.  
  86.   /* Open the Intuition library: */
  87.   IntuitionBase = (struct IntuitionBase *)
  88.     OpenLibrary( "intuition.library", 0 );
  89.   if( !IntuitionBase )
  90.     clean_up( "Could NOT open the Intuition library!" );
  91.  
  92.   /* Open the Graphics library: */
  93.   GfxBase = (struct GfxBase *)
  94.     OpenLibrary( "graphics.library", 0 );
  95.   if( !GfxBase )
  96.     clean_up( "Could NOT open the Graphics library!" );
  97.  
  98.  
  99.   /* Save the current View, so we can restore it later: */
  100.   my_old_view = GfxBase->ActiView;
  101.  
  102.  
  103.   /* 1. Prepare the View structure, and give it a pointer to */
  104.   /*    the first ViewPort:                                  */
  105.   InitView( &my_view );
  106.   my_view.ViewPort = &my_view_port;
  107.  
  108.  
  109.   /* 2. Prepare the ViewPort structure, and set some important values: */
  110.   InitVPort( &my_view_port );
  111.   my_view_port.DWidth = DWIDTH;        /* Set the width.                */
  112.   my_view_port.DHeight = DHEIGHT;      /* Set the height.               */
  113.   my_view_port.DxOffset = DXOFFSET;    /* Set the display X offset.     */
  114.   my_view_port.DyOffset = DYOFFSET;    /* Set the display Y offset.     */
  115.   my_view_port.RasInfo = &my_ras_info; /* Give it a pointer to RasInfo. */
  116.   my_view_port.Modes = NULL;           /* Low resolution.               */
  117.  
  118.  
  119.   /* 3. Get a colour map, link it to the ViewPort, and prepare it: */
  120.   my_view_port.ColorMap = (struct ColorMap *) GetColorMap( COLOURS );
  121.   if( my_view_port.ColorMap == NULL )
  122.     clean_up( "Could NOT get a ColorMap!" );
  123.  
  124.   /* Get a pointer to the colour map: */
  125.   pointer = (UWORD *) my_view_port.ColorMap->ColorTable;
  126.  
  127.   /* Set the colours: */
  128.   for( loop = 0; loop < COLOURS; loop++ )
  129.     *pointer++ = my_color_table[ loop ];
  130.  
  131.  
  132.   /* 4. Prepare the BitMap: */
  133.   InitBitMap( &my_bit_map, DEPTH, RWIDTH, RHEIGHT );
  134.  
  135.   /* Allocate memory for the Raster: */ 
  136.   for( loop = 0; loop < DEPTH; loop++ )
  137.   {
  138.     my_bit_map.Planes[ loop ] = (PLANEPTR) AllocRaster( RWIDTH, RHEIGHT );
  139.     if( my_bit_map.Planes[ loop ] == NULL )
  140.       clean_up( "Could NOT allocate enough memory for the raster!" );
  141.  
  142.     /* Clear the display memory with help of the Blitter: */
  143.     BltClear( my_bit_map.Planes[ loop ], RASSIZE( RWIDTH, RHEIGHT ), 0 );
  144.   }
  145.  
  146.   
  147.   /* 5. Prepare the RasInfo structure: */
  148.   my_ras_info.BitMap = &my_bit_map; /* Pointer to the BitMap structure.  */
  149.   my_ras_info.RxOffset = 0;         /* The top left corner of the Raster */
  150.   my_ras_info.RyOffset = 0;         /* should be at the top left corner  */
  151.                                     /* of the display.                   */
  152.   my_ras_info.Next = NULL;          /* Single playfield - only one       */
  153.                                     /* RasInfo structure is necessary.   */
  154.  
  155.   /* 6. Create the display: */
  156.   MakeVPort( &my_view, &my_view_port );
  157.   MrgCop( &my_view );
  158.  
  159.  
  160.   /* 7. Prepare the RastPort, and give it a pointer to the BitMap. */
  161.   InitRastPort( &my_rast_port );
  162.   my_rast_port.BitMap = &my_bit_map;
  163.   
  164.  
  165.   /* 8. Show the new View: */
  166.   LoadView( &my_view );
  167.  
  168.  
  169.   /* Set the draw mode to JAM1. FgPen's colour will be used. */
  170.   SetDrMd( &my_rast_port, JAM1 );
  171.   /* Draw 10000 pixels in seven different colours, randomly. */ 
  172.   for( loop = 0; loop < 10000; loop++ )
  173.   {
  174.     /* Set FgPen's colour (1-7, 0 used for the the background). */
  175.     SetAPen( &my_rast_port, rand() % (COLOURS-1) + 1 );
  176.     /* Write a pixel somewere on the display: */
  177.     WritePixel( &my_rast_port, rand() % RWIDTH, rand() % RHEIGHT );
  178.   }
  179.  
  180.  
  181.   /* Scroll the Raster in all directions for a little while: */
  182.   for( loop = 0; loop < 5000; loop++ )
  183.   {
  184.     my_ras_info.RxOffset += deltaX;
  185.     my_ras_info.RyOffset += deltaY;
  186.  
  187.     /* The Raster is moved in one direction until the other side is */
  188.     /* reached were we change the direction:                        */ 
  189.  
  190.     /* Have we reached the left side? */
  191.     if( my_ras_info.RxOffset <= 0 )
  192.       deltaX = SPEED;
  193.     /* Have we reached the right (Raster width - Display width) side? */
  194.     if( my_ras_info.RxOffset >= RWIDTH - DWIDTH )
  195.       deltaX = -SPEED;
  196.  
  197.     /* Have we reached the top side? */
  198.     if( my_ras_info.RyOffset <= 0 )
  199.       deltaY = SPEED;
  200.     /* Have we reached the bottom (Raster height - Display height) side? */
  201.     if( my_ras_info.RyOffset >= RHEIGHT - DHEIGHT )
  202.       deltaY = -SPEED;
  203.  
  204.  
  205.     /* Recalculate the display instructions: (If you change any values */
  206.     /* in the display structures the Amiga have to recalculate the     */
  207.     /* entire display instructions. You must therefore call all three  */
  208.     /* display functions: MakeVPort(), MrgCop() and LoadView().)       */
  209.     MakeVPort( &my_view, &my_view_port );
  210.     MrgCop( &my_view );
  211.     LoadView( &my_view );
  212.   }
  213.  
  214.  
  215.   /* 9. Restore the old View: */
  216.   LoadView( my_old_view );
  217.  
  218.  
  219.   /* Free all allocated resources and leave. */
  220.   clean_up( "THE END" );
  221. }
  222.  
  223.  
  224. /* Returns all allocated resources: */
  225. void clean_up( message )
  226. STRPTR message;
  227. {
  228.   int loop;
  229.  
  230.   /* Free automatically allocated display structures: */
  231.   FreeVPortCopLists( &my_view_port );
  232.   FreeCprList( my_view.LOFCprList );
  233.   
  234.   /* Deallocate the display memory, BitPlane for BitPlane: */
  235.   for( loop = 0; loop < DEPTH; loop++ )
  236.     if( my_bit_map.Planes[ loop ] )
  237.       FreeRaster( my_bit_map.Planes[ loop ], RWIDTH, RHEIGHT );
  238.  
  239.   /* Deallocate the ColorMap: */
  240.   if( my_view_port.ColorMap ) FreeColorMap( my_view_port.ColorMap );
  241.  
  242.   /* Close the Graphics library: */
  243.   if( GfxBase ) CloseLibrary( GfxBase );
  244.  
  245.   /* Close the Intuition library: */
  246.   if( IntuitionBase ) CloseLibrary( IntuitionBase );
  247.  
  248.   /* Print the message and leave: */
  249.   printf( "%s\n", message ); 
  250.   exit();
  251. }
  252.